<?php
namespace app\admin\controller;

//引入公共控制器
use app\common\controller\Backend;
use Think\Db;

/**
 * 系统配置的控制器
 */
class Config extends Backend
{
    public function __construct()
    {
        //继承父类
        parent::__construct();

        $this->ConfigModel = model('Config');
    }
    
    /**
     * 配置列表
     */
    public function index()
    {
        $count = $this->ConfigModel->count();

        $conflist = $this->ConfigModel->paginate(10);

        $pagestr = $conflist->render();

        $this->assign([
            'count' => $count,
            'conflist' => $conflist,
            'pagestr' => $pagestr,
        ]);

        return $this->fetch();
    }

    /**
     * 添加系统配置
     */
    public function add()
    {
        if($this->request->isPost())
        {
            $title = $this->request->param('title', '');
            $key = $this->request->param('key', '');
            $value = $this->request->param('value', '');
            $type = $this->request->param('type', '');

            //组装数据
            $data = [
                'title' => $title,
                'key' => $key,
                'type' => $type,
            ];

            //说明配置的类型是文本
            if($type == "text")
            {
                $data['value'] = $value;
            }else if($type == "file")
            {
                //说明配置是文件
                $result = upload('file');
                
                if($result === FALSE)
                {
                    $this->error('上传文件失败');
                    exit;
                }

                //组装数据
                $data['value'] = '/uploads/'.$result;
            }

            //插入数据库
            $result = $this->ConfigModel->validate('common/Config')->save($data);

            if($result === FALSE)
            {
                $this->error($this->ConfigModel->getError());
                exit;
            }else
            {
                $this->success('添加系统配置成功', url('admin/config/index'));
                exit;
            }
        }
        return $this->fetch();
    }

    public function edit()
    {
        //获取配置选项的id
        $conid = $this->request->param('conid', 0);

        $row = $this->ConfigModel->find($conid);

        if(!$row)
        {
            $this->error('无配置选项');
            exit;
        }

        if($this->request->isPost())
        {
            $title = $this->request->param('title', '');
            $value = $this->request->param('value', '');
            $type = $this->request->param('type', '');

            //组装数据
            $data = [
                'id' => $conid,
                'title' => $title,
                'type' => $type,
            ];

            //说明配置的类型是文本
            if($type == "text")
            {
                $data['value'] = $value;
            }else if($type == "file")
            {
                //判断是否有文件上传
                if($_FILES['file']['size'] > 0)
                {
                    //说明配置是文件
                    $result = upload('file');
                    
                    if($result === FALSE)
                    {
                        $this->error('上传文件失败');
                        exit;
                    }

                    //组装数据
                    $data['value'] = '/uploads/'.$result;
                } 
            }

            //更新数据库
            $result = $this->ConfigModel->isUpdate(true)->save($data);

            if($result === FALSE)
            {
                $this->error($this->ConfigModel->getError());
                exit;
            }else
            {
                //如果是有上传新文件，需要先把旧文件删除
                //判断是否有文件上传
                if($data['type'] == "file" && $_FILES['file']['size'] > 0)
                {
                    $old = ltrim($row['value'], "/");
                    @is_file($old) && @unlink($old);
                }

                $this->success('更新系统配置成功', url('admin/config/index'));
                exit;
            }
        }




        $this->assign('row', $row);

        return $this->fetch();
    }

    public function del()
    {
        $success = [
            'result' => false,
            'msg' => ''
        ];
        
        if($this->request->isAjax())
        {
            $ids = $this->request->param('ids', 0);

            $result = $this->ConfigModel->destroy($ids);

            if($result === FALSE)
            {
                $success['result'] = false;
                $success['msg'] = $this->ConfigModel->getError();
            }else
            {
                $success['result'] = true;
                $success['msg'] = '删除配置成功';
            }

            return json($success);
        }
    }
}
